<template>
    <div class="pageMain">
        <div class="logoBox">
            <img src="../../assets/logo.png" alt="">
        </div>

        <div class="infoBox">
            <div>
                <span>账号：</span><input type="text" v-model="account" placeholder="请输入账号">
            </div>
            <div>
                <span>密码：</span><input type="text" v-model="password" placeholder="请输入密码">
            </div>
        </div>

        <div class="loginBtn" @click="loginHandle">登录</div>


        <div class="loginBtn" @click="authLogin">微信登录</div>
        <!-- <div class="loginBtn" @click="authUserInfo">用户信息</div> -->
    </div>
</template>

<script>

import { telLogin, wxLogin } from '../../network/logIn'

let that
export default {
    name: 'HomeR',
    data() {
        return {
            account: '',
            password: '',
        }
    },
    mounted() {

    },
    methods: {
        loginHandle() {

            if (this.account == '') {
                this.$toast('请输入账号')
                return
            }

            if (this.password == '') {
                this.$toast('请输入密码')
                return
            }

            console.log(this.account);
            console.log(this.password);

            telLogin({
                tel: this.account,
                password: this.password,
            }).then(res => {
                if (res.flag) {
                    console.log(JSON.stringify(res));
                    console.log('哈哈哈哈');

                    if (res.data.flag == 'success') {
                        var user = res.data.user
                        localStorage.user = JSON.stringify(user)
                        localStorage.userId = user.userId
                        localStorage.isVip = user.isVip// 0 用户  1 会员
                        localStorage.userType = user.userType// 0 用户  1 医生  2 助理
                        localStorage.myDoctorId = user.doctorId//  我的医生ID


                        localStorage.myDoctorName = user.doctorName
                        localStorage.myHelperName = user.helperName


                        // return

                        var url = user.userType != 2 ? '/index' : '/aideIndex'
                        this.$router.replace(url)

                    } else {
                        this.$toast(res.data.msg)
                    }

                }
            })



        },

        authLogin() {

            s = auths[1];

            console.log(JSON.stringify(s));
            if (!s.authResult) {

                s.login(function (e) {
                    console.log(JSON.stringify(e));

                    console.log("登录认证成功！");
                    s.getUserInfo(function (e) {
                        // var josnStr = JSON.stringify(s.userInfo);
                        // infos = josnStr;


                        console.log(e);
                        console.log(s);


                        console.log("获取用户信息成功：" + JSON.stringify(s.userInfo));
                        var infos = s.userInfo
                        var userInfoStr = JSON.stringify(infos)

                        console.log(infos.unionid);
                        console.log(infos.openid);
                        console.log(userInfoStr);
                        that.loginWXHandle(infos)





                        // console.log("获取用户信息成功：" + JSON.stringify(s.userInfo));
                    }, function (e) {

                        console.log("获取用户信息失败：" + e.message + " - " + e.code);

                    });

                }, function (e) {

                    console.log("登录认证失败！");

                });


            } else {

                console.log("已经登录认证！");

            }

        },

        // 获取登录用户信息操作

        authUserInfo() {

            var s = auths[0];

            if (!s.authResult) {

                alert("未授权登录！");

            } else {

                s.getUserInfo(function (e) {

                    alert("获取用户信息成功：" + JSON.stringify(e.userInfo));

                }, function (e) {

                    alert("获取用户信息失败：" + e.message + " - " + e.code);

                });

            }

        },
        loginWXHandle(infos) {
            console.log('进方法');
            console.log(JSON.stringify(infos));
            wxLogin({
                // doctorId: 0,
                // helperId: 0,
                unionId: infos.unionid,
                openId: infos.openid,
                userName: infos.nickname,
                userSex: infos.sex == 0 ? '男' : '女',
                userImg: infos.headimgurl,
            }).then(res => {
                // console.log(res);
                // console.log(JSON.stringify(res));
                if (res.flag) {
                    console.log('进方11');

                    console.log(res);
                    console.log(JSON.stringify(res));

                    this.$toast('登录成功')

                    var user = res.data.user
                    localStorage.user = JSON.stringify(user)
                    localStorage.userId = user.userId
                    localStorage.isVip = user.isVip// 0 用户  1 会员
                    localStorage.userType = user.userType// 0 用户  1 医生  2 助理
                    localStorage.myDoctorId = user.doctorId//  我的医生ID

                    localStorage.myDoctorName = user.doctorName
                    localStorage.myHelperName = user.helperName



                    // return

                    var url = user.userType != 2 ? '/index' : '/aideIndex'
                    this.$router.replace(url)


                }


            })
        },


    },
    created() {

        // if (localStorage.user) {
        //     var user = JSON.parse(localStorage.user)
        //     var url = user.userType != 2 ? '/index' : '/aideIndex'
        //     this.$router.replace(url)
        // }
        that = this



    },
}





</script>

<style lang='less' scoped>
.pageMain {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
}

.logoBox {
    width: 120px;
    height: 120px;
    border-radius: 20px;
    background: #0aec4e;
}

.logoBox>img {
    width: 100%;
    height: 100%;
}


.infoBox {
    position: relative;
    margin: 160px 0 100px;
    box-sizing: border-box;
    padding: 0 48px;
    width: 100%;
    font-size: 28px;
    color: #333;
}

.infoBox>div {
    display: flex;
    align-items: center;
    height: 104px;
    border-bottom: 2px solid #f2f2f2;
}

.infoBox>div>input {
    margin-left: 20px;
    border: none;
    outline: none;

}

.loginBtn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 654px;
    height: 88px;
    background: #039e31;
    border-radius: 10px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #FFFFFF;
}
</style>